package com.cf.controller;

import com.cf.entity.Result;
import com.cf.entity.User;
import com.cf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private UserService userService;

//    登录
    @RequestMapping("/login")
    public Object login(String username,String password){
        User user = userService.login(username);
        if(user==null){
            return new Result(400,"用户不存在",null);
        }else {
            if(!user.getPassword().equals(password)){
                return new Result(400,"密码错误",null);
            }else {
                return new Result(200,"管理员登录成功",user);
            }
        }
    }
//    注册
    @RequestMapping("/register")
    public Object register(User user){
        int i = userService.register(user);
        if (i==0){
            return new Result(400,"添加失败",null);
        }else {
            return new Result(200,"添加成功",null);
        }
    }
}
